
using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using System.ComponentModel;
using System.Web.SessionState;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Net;
using com.ddsc.common;
using com.ddsc.database;
using com.ddsc.utility;
using System.Text;


//Namespace required.
namespace smasb2b
{
	public class ReportViewer : System.Web.UI.Page
	{
		
		ReportDocument rd = new ReportDocument();
		protected CrystalDecisions.Web.CrystalReportViewer CRV1;
		protected System.Web.UI.HtmlControls.HtmlInputHidden txtSEQ;
		ReportDocument subreport = new ReportDocument();
		
#region  Web Form 設計工具產生的程式碼
		
		//此為 Web Form 設計工具所需的呼叫。
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		
		//注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
		//請勿刪除或移動它。
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
			//請勿使用程式碼編輯器進行修改。
			InitializeComponent();
		}
		
#endregion
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			
			//在這裡放置使用者程式碼以初始化網頁
			dbConnection dba = new dbConnection();
			
			int i = 0;
			int cut;
			int j = 0;
			string ReportName = "";
			
			SubreportObject subreportObject;
			string subreportName = "";
			CrystalDecisions.Shared.TableLogOnInfo myLoginInfo = new CrystalDecisions.Shared.TableLogOnInfo();
			TableLogOnInfo logOnInfo = new TableLogOnInfo();
			
			string strIni;
			
			ParameterValues pv = new ParameterValues();
			ParameterDiscreteValue pdv = new ParameterDiscreteValue();
			int LINT_I = 0;
			try
			{
				ReportName = Request["ReportName"];
				strIni = new string(' ', 100);
				//ReportName = "/BAS/Report/" + ReportName
				if (File.Exists(ReportName))
				{
					rd.Load(ReportName);
				}
				else
				{
					rd.Load(Server.MapPath("") + ReportName);
				}
				//2010/07/07 added by Ray Reload report and to prevent report cache previous data
				rd.Refresh();
				
				// rd.OpenSubreport("CR3subreport")
				CrystalDecisions.CrystalReports.Engine.Table table = default(CrystalDecisions.CrystalReports.Engine.Table);
				foreach (CrystalDecisions.CrystalReports.Engine.Table tempLoopVar_table in rd.Database.Tables)
				{
					table = tempLoopVar_table;
					if (rd.Database.Tables[i].Location != "data")
					{
						logOnInfo = table.LogOnInfo;
						logOnInfo.ConnectionInfo.AllowCustomConnection = true;
						logOnInfo.ConnectionInfo.ServerName = dba.getServerName();
						logOnInfo.ConnectionInfo.DatabaseName = dba.getDbName();
						logOnInfo.ConnectionInfo.UserID = dba.getId();
						logOnInfo.ConnectionInfo.Password = dba.getPassword();
						table.ApplyLogOnInfo(logOnInfo);
						rd.Database.Tables[i].Location = table.Location;
						rd.DataSourceConnections[dba.getServerName(), dba.getDbName()].SetConnection(dba.getServerName(), dba.getDbName(), dba.getId(), dba.getPassword());
					}
					i++;
				}
				
				for (j = 0; j <= rd.Subreports.Count - 1; j++)
				{
					subreportName = rd.Subreports[j].Name;
					subreport = rd.OpenSubreport(subreportName);
					i = 0;
					foreach (CrystalDecisions.CrystalReports.Engine.Table tempLoopVar_table in subreport.Database.Tables)
					{
						table = tempLoopVar_table;
						logOnInfo = table.LogOnInfo;
						logOnInfo.ConnectionInfo.AllowCustomConnection = true;
						logOnInfo.ConnectionInfo.ServerName = dba.getServerName();
						logOnInfo.ConnectionInfo.DatabaseName = dba.getDbName();
						logOnInfo.ConnectionInfo.UserID = dba.getId();
						logOnInfo.ConnectionInfo.Password = dba.getPassword();
						table.ApplyLogOnInfo(logOnInfo);
						subreport.Database.Tables[i].Location = table.Location;
						subreport.DataSourceConnections[dba.getServerName(), dba.getDbName()].SetConnection(dba.getServerName(), dba.getDbName(), dba.getId(), dba.getPassword());
						i++;
					}
				}
				
				
				
				if (!(Request["seq"] == null))
				{
					txtSEQ.Value = Strings.Trim(Request["seq"]);
				}
				//此段會抓到子報表的參數，所以不準確, 必需判斷參數有沒有值
				//rd.DataDefinition.ParameterFields.Count
				for (LINT_I = 0; LINT_I <= rd.DataDefinition.ParameterFields.Count - 1; LINT_I++)
				{
					if (Request["Param" + System.Convert.ToString(LINT_I)] == null)
					{
						break;
					}
					
					pdv.Value = Server.UrlDecode(Request["Param" + System.Convert.ToString(LINT_I)]);
					pv.Add(pdv);
					rd.DataDefinition.ParameterFields[LINT_I].ApplyCurrentValues(pv);
					
				}
				//CRV1.SeparatePages = False
				if (!Page.IsPostBack)
				{
					CRV1.DisplayGroupTree = false;
				}
				CRV1.EnableDrillDown = false;
				CRV1.ReportSource = rd;
				
				//rd.Close()
				//rd.Dispose()
				
				
			}
			catch (Exception ex)
			{
				Response.Write(ex.Message);
			}
			finally
			{
			}
			
		}
		private void Page_unLoad(System.Object sender, System.EventArgs e)
		{
			rd.Close();
			rd.Dispose();
		}
		
		
	}
}
